﻿Public Class UCQLPhieuThue
    Dim PhieuThueDAO As New PhieuThueDAO()
    Dim PhieuThueList As New List(Of PhieuThueDTO)
    Dim flag As Boolean = True
    Dim connect As New connect()
    Private Sub UCQLPhieuThue_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        LoadTT()
        dgvPhieuThue.ClearSelection()
    End Sub
    Private Sub LoadTT()
        flag = False
        dgvPhieuThue.ClearSelection()
        PhieuThueList = PhieuThueDAO.GetData()
        dgvPhieuThue.DataSource = PhieuThueList
        dgvPhieuThue.ClearSelection()
        flag = True
    End Sub
    Private Sub dgvPhieuThue_SelectionChanged(sender As System.Object, e As System.EventArgs) Handles dgvPhieuThue.SelectionChanged
        flag = False
        txtMaPhieuThue.Text = dgvPhieuThue.CurrentRow.Cells(0).Value.ToString()
        txtMaKH.Text = dgvPhieuThue.CurrentRow.Cells(1).Value.ToString()
        txtNgayThue.Text = dgvPhieuThue.CurrentRow.Cells(2).Value.ToString()
        txtMaTinDang.Text = dgvPhieuThue.CurrentRow.Cells(3).Value.ToString()
        txtGiaTien.Text = dgvPhieuThue.CurrentRow.Cells(4).Value.ToString()
        txtDonVi.Text = dgvPhieuThue.CurrentRow.Cells(5).Value.ToString()
        cbBiXoa.Checked = Convert.ToBoolean(dgvPhieuThue.CurrentRow.Cells(6).Value)
        cbBiXoa.Enabled = True
        btnXoa.Enabled = True
        flag = True
    End Sub
    Private Sub LamMoi()
        LoadTT()
        flag = False
        txtMaPhieuThue.Text = ""
        txtMaKH.Text = ""
        txtNgayThue.Text = ""
        txtMaTinDang.Text = ""
        txtGiaTien.Text = ""
        txtDonVi.Text = ""
        cbBiXoa.Checked = False
        cbBiXoa.Enabled = False
        btnXoa.Enabled = False
        flag = True
    End Sub
    Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
        LamMoi()
    End Sub

    Private Sub btnXoa_Click(sender As System.Object, e As System.EventArgs) Handles btnXoa.Click
        Dim sql As String
        If (txtMaTinDang.Text <> "") Then
            Dim result = MessageBox.Show("Để tránh sai lệnh dữ liệu. Bạn có muốn đánh dấu Xoá không?", "Cảnh báo", MessageBoxButtons.YesNo)
            If result = DialogResult.No Then
                sql = "delete PHIEUTHUE where MAPT='" + txtMaPhieuThue.Text + "'"
                Dim sqlupdate As String = "update TINDANG set DATHUE='Flase' where MATD='" + txtMaTinDang.Text + "'"
                connect.ExecSql(sql)
                connect.ExecSql(sqlupdate)
                MessageBox.Show("Xoá thành công!")
                LoadTT()
                LamMoi()
            ElseIf result = DialogResult.Yes Then
                sql = "update PHIEUTHUE set BIXOA='True' where MAPT='" + txtMaPhieuThue.Text + "'"
                connect.ExecSql(sql)
                MessageBox.Show("Đánh dấu xoá thành công!")
                LoadTT()
                LamMoi()
            End If
        End If

    End Sub

    Private Sub cbBiXoa_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles cbBiXoa.CheckedChanged
        If (flag) Then
            Dim sql As String
            If (cbBiXoa.Checked) Then
                sql = "update PHIEUTHUE set BIXOA='True' where MAPT='" + txtMaPhieuThue.Text + "'"
                connect.ExecSql("update TINDANG set DATHUE='True' where MATD='" + txtMaTinDang.Text + "'")
            Else
                sql = "update PHIEUTHUE set BIXOA='False' where MAPT='" + txtMaPhieuThue.Text + "'"
                connect.ExecSql("update TINDANG set DATHUE='False' where MATD='" + txtMaTinDang.Text + "'")
            End If
            connect.ExecSql(sql)
            MessageBox.Show("Đã lưu thay đổi")
            LoadTT()
            LamMoi()
        End If
    End Sub
End Class
